/**
 * @file   PacketType.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-20
 * @brief  DON packet type enum and it's supporting functions.
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/Packet/PacketType.h>

using namespace DON::Core;

bool Packet::isPacketTypeValid(const PacketType packetType)
{
    bool success = false;

    if ((packetType >= PacketType_RegistrationRequest) &&
        (packetType <= PacketType_EventConfirmation))
    {
        success = true;
    }

    return success;
}

Packet::PacketType Packet::fromBinaryToPacketType(const char data)
{
    PacketType packetType = PacketType_Invalid;

    if ((data >= static_cast<char>(PacketType_RegistrationRequest)) &&
        (data <= static_cast<char>(PacketType_EventConfirmation)))
    {
        packetType = static_cast<PacketType>(data);
    }

    return packetType;
}

char Packet::fromPacketTypeToBinary(const PacketType packetType, bool *ok)
{
    char value = 0;
    bool success = false;

    if (isPacketTypeValid(packetType))
    {
        value = static_cast<char>(packetType);
        success = true;
    }

    if (ok != NULL)
    {
        *ok = success;
    }

    return value;
}
